package com.binfenhulian.chinaenergy.ces.utils.file;

import java.io.*;

public class FileUtil
{
  public static void writeFile(File file, byte[] buffer)
  {
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    try {
      fos = new FileOutputStream(file);
      bos = new BufferedOutputStream(fos);

      bos.write(buffer);
      bos.flush();
    } catch (Exception e) {
      e.printStackTrace();
      try
      {
        if (bos != null) {
          bos.close();
        }
        if (fos != null)
          fos.close();
      }
      catch (Exception e1) {
        e1.printStackTrace();
      }
    }
    finally
    {
      try
      {
        if (bos != null) {
          bos.close();
        }
        if (fos != null)
          fos.close();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  public static byte[] readFile(File file, int fileLength)
  {
    FileInputStream fis = null;
    byte[] buffer = new byte[fileLength];
    try {
      fis = new FileInputStream(file);
      fis.read(buffer);

      fis.close();
    } catch (Exception e) {
      e.printStackTrace();
      try
      {
        if (fis != null)
          fis.close();
      }
      catch (IOException e1) {
        e1.printStackTrace();
      }
    }
    finally
    {
      try
      {
        if (fis != null)
          fis.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
    return buffer;
  }
}